//
//  SDReadTitleLoadView.m
//  SBooks
//
//  Created by shansander on 2016/11/15.
//  Copyright © 2016年 shansander. All rights reserved.
//

#import "SDReadTitleLoadView.h"
#import "AppMacro.h"
#import "SDAnimationCompiler.h"

CGFloat const define_nativecontrollerbar_height = 64.0f;

@implementation SDReadTitleLoadView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
        
        self.backgroundColor = COLOR(239, 238, 233, 1);
        
        [self configView];
    }
    return self;
}

- (instancetype)initWithBackBlock:(BackActionBlock)back menuBlock:(MenuActionBlock)menu
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, screenWidth, screenHeight);
        
        self.backgroundColor = COLOR(239, 238, 233, 1);
        
        [self configView];
        
        _backBlock = back;
        _menuBlock = menu;

    }
    return self;
}

- (void)configView
{
    theTitleLabel = [[UILabel alloc] init];
    theTitleLabel.textColor = COLOR(120, 120, 120, 1);
    [self addSubview:theTitleLabel];
    theTitleLabel.center = CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f);
    
    theBackButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [theBackButton setBackgroundColor:[UIColor clearColor]];
    [theBackButton setTintColor:COLOR(161, 156, 133, 1)];
    [self addSubview:theBackButton];
    UIImage * backImage = [UIImage imageNamed:@"backward-arrow"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [theBackButton setImage:backImage forState:UIControlStateNormal];
    
    [theBackButton setImageEdgeInsets:UIEdgeInsetsMake(12.5, 12.5, 12.5, 12.5)];
    [theBackButton setCenter:CGPointMake(20, 44)];
    [theBackButton addTarget:self action:@selector(onBackAction:) forControlEvents:UIControlEventTouchUpInside];
    //menu-button
    theMenuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    theMenuButton.center = CGPointMake(self.bounds.size.width - 20.0f, 44);
    [theMenuButton setTintColor:COLOR(161, 156, 133, 1)];
    [self addSubview:theMenuButton];
    UIImage * menuImage = [UIImage imageNamed:@"shared_icon"];
    menuImage = [menuImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [theMenuButton setImageEdgeInsets:UIEdgeInsetsMake(10, 12.5, 10, 12.5)];
    [theMenuButton setImage:menuImage forState:UIControlStateNormal];
    [theMenuButton addTarget:self action:@selector(onMenuAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [theBackButton setAlpha:0.0f];
    [theMenuButton setAlpha:0.0f];
    
}


- (void)setTitle:(NSString *)title
{
    _title = title;
    theTitleLabel.text = self.title;
    
    CGFloat textSizeWidth = screenWidth/2.0f;
    CGRect textFrame = [title boundingRectWithSize:CGSizeMake(textSizeWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    theTitleLabel.frame = textFrame;
    theTitleLabel.center = CGPointMake(self.bounds.size.width/2.0f, self.bounds.size.height/2.0f);
}


- (void)tapChangeTitleView
{
    [theBackButton setCenter:CGPointMake(theBackButton.center.x, theBackButton.center.y + 20)];
    [theMenuButton setCenter:CGPointMake(theMenuButton.center.x, theMenuButton.center.y + 20)];
    
    [theTitleLabel pop_addAnimation:[SDAnimationCompiler getAnimation_hideCenterTitleLabel] forKey:@"hidecentertitlelabel"];
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, 0, screenWidth, define_nativecontrollerbar_height);
    } completion:^(BOOL finished) {
        if (finished) {
            theTitleLabel.center = CGPointMake(self.bounds.size.width/2.0f, 64);
            [theBackButton pop_addAnimation:[SDAnimationCompiler getAnimation_showTextWithAlpha] forKey:@"showtextalpha1"];
            [theMenuButton pop_addAnimation:[SDAnimationCompiler getAnimation_showTextWithAlpha] forKey:@"showtextalpha2"];
            [theTitleLabel pop_addAnimation:[SDAnimationCompiler getAnimation_showTextWithAlpha] forKey:@"showtextalpha3"];
            [theBackButton pop_addAnimation:[SDAnimationCompiler getAnimation_showTextWithCenter:theBackButton.center] forKey:@"movebackanimtion1"];
            [theMenuButton pop_addAnimation:[SDAnimationCompiler getAnimation_showTextWithCenter:theMenuButton.center] forKey:@"movemenuanimation2"];
            [theTitleLabel pop_addAnimation:[SDAnimationCompiler getAnimation_showTextWithCenter:theTitleLabel.center] forKey:@"movetitleanimtion3"];
        }
       
    }];
    
    
//    theTitleLabel.center = CGPointMake(self.bounds.size.width/2.0f, 44);
}

- (void)onBackAction:(id)sender
{
    if (self.backBlock) {
        self.backBlock(sender);
    }
}
- (void)onMenuAction:(id)sender
{
    if (self.menuBlock) {
        self.menuBlock(sender);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
